package top.roadvast.springboot.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.logging.SimpleFormatter;

/**
 * @Author: guozepeng
 * @CreateTime: 2011/10/19
 * @Description:
 *           1、计算两个日期之间相隔几天
 *           2、通过时间戳获取日期
 *           3、通过日期获取时间戳
 *           4、获取当前系统的时间
 *           5、测试
 */
public class TimeConversionUtil {
    //计算两个日期直接相隔几天
    public static int daysBetweenCount(String strTime1, String strTime2) {
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date1 = LocalDate.parse(strTime1, ftf);
        LocalDate date2 = LocalDate.parse(strTime2, ftf);
        return Math.toIntExact(date1.until(date2, ChronoUnit.DAYS));
    }

    //传入long时间戳 ，转换成格式化的String类型时间
    public static String timeOfLongToStr(long time) {
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
    }
 
    //传入String类型格式化时间，转换成Long类型的时间戳
    public static long timeOfStrToLong(String strTime){
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(strTime, ftf);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }
 
 
    //通过LocalDateTime获取当前格式化时间
    public static String getTimeStrNow(){
        //DateTimeFormatter替换了SimpleDateFormat, 因为线程安全问题。
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
 
    //通过LocalDateTime获取当前格式化时间
    public static String getTimeStrNow1(){
        //DateTimeFormatter替换了SimpleDateFormat, 因为线程安全问题。
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    //将时间格式化
    public static String getFormatDate(Date date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        return localDateTime.format(formatter);
    }
 
    //获取当前时间戳方法一
    public long getTimeLongOfClock(){
        return Clock.systemDefaultZone().millis();
    }
 
    //获取当前时间戳方法二
    public long getTimeLongOfCalendar(){
        Calendar calendar = Calendar.getInstance();
        return calendar.getTimeInMillis();
    }
 
    //获取当前时间戳方法三
    public long getTimeLongOfSystem(){
        return System.currentTimeMillis();
    }
 
    //获取当前时间戳方法四
    public long getTimeLongOfDate(){
        Date date = new Date();
        return date.getTime();
    }
 
    //获取当前时间戳五 精确到毫秒
    public  static long getTimeLongOfInstant(){
        Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(0));
        //   System.out.println("秒数:"+now.getEpochSecond());
        //   System.out.println("毫秒数:"+now.toEpochMilli());
        return now.toEpochMilli();
    }
    //获取当前时间戳六 精确到秒
    public static long getTimeLongOfInstantEpochSecond(){
        Instant now = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(0));
        //   System.out.println("秒数:"+now.getEpochSecond());
        //   System.out.println("毫秒数:"+now.toEpochMilli());
        long longTimeNow=now.getEpochSecond();
        return longTimeNow;
    }

    //获取当前系统时间一
    public static Date getNow() {
        return Calendar.getInstance().getTime();
    }

    //工具类调试
    public static void main(String[] args) {
        String timeStrNow = getTimeStrNow();
        System.out.println(timeStrNow);
        String s = timeOfLongToStr(getTimeLongOfInstant());
        System.out.println(s);
    }
}
 